﻿using Batman.Core;
using Batman.Jianbei.Forum.Core.Datas;
using Microsoft.EntityFrameworkCore;
using Quartz;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Batman.Jianbei.Forum.Jobs
{
    public class MemberIsInRoomCheckJob : IJob
    {
        public Task Execute(IJobExecutionContext context)
        {
            using (var ctx = new ChatRoomDbFactory().CreateDbContext(null))
            {
                var now = DateTime.Now;
                // 获取房间没注销的且数据库中还是在房间状态的但是上次报到是2分钟前的
                var query = ctx.RoomMask.AsNoTracking().Include(t => t.Room)
                                                      .Where(t => t.IsInRoom)
                                                      .Where(t => !t.Room.IsDismiss)
                                                      .Where(t => t.CheckIn.AddMinutes(2) < now)
                                                      ;
                var leaved = query.ToList();
                leaved.ForEach(t => t.IsInRoom = false); // 设置状体为不在房间

                ctx.RoomMask.UpdateRange(leaved);
                ctx.SaveChanges();

                return Task.CompletedTask;
            }
        }
    }
}
